package com.yuanyi.controller;

import com.yuanyi.dao.BaoXiuDanDao;
import com.yuanyi.dao.RepairManDao;
import com.yuanyi.entity.BaoXiuDan;
import com.yuanyi.entity.BaoXiuMan;
import com.yuanyi.entity.Equipment;
import com.yuanyi.entity.RepairMan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
public class RepairManController {
    @Autowired
    RepairManDao repairManDao;
    @Autowired
    BaoXiuDanDao baoXiuDanDao;
    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @GetMapping("tel_isrepeat")
    public boolean isrepeat(String tel) {
        return repairManDao.findRepairManByTel(tel) == null;
    }

    @GetMapping("/find_repairman_byopenid")
    public RepairMan findRepairManByOpenid(String openid) {
        return repairManDao.findRepairManByOpenid(openid);
    }

    @PostMapping("repair_man_bind")
    public RepairMan bangding(@RequestBody RepairMan repairMan) {
        String yzm = repairMan.getYzm();

        String redisYZM = (String) redisTemplate.opsForValue().get(repairMan.getTel());
        if (!yzm.equals(redisYZM)) {
            return null;
        }

        if (repairMan.getOpenid().length() == 0) {
            repairMan.setOpenid(UUID.randomUUID().toString());
        }

        RepairMan r = null;
        try {
            r = repairManDao.save(repairMan);
        } catch (Exception ex) {
            if (ex instanceof DuplicateKeyException) {
                return null;
            }
        }

        return r;
    }

    @GetMapping("/find_top_repair_man")
    public Page<RepairMan> findAllwx(Integer p, int size) {
        Sort sort = new Sort(Sort.Direction.ASC, "id");
//
        PageRequest pr = PageRequest.of(p - 1, size, sort);
        // Pageable pageable = new PageRequest(p - 1, size);
        return repairManDao.findAll(pr);
    }

    //查找维修记录
    @GetMapping("/find_wxhistory")
    @ResponseBody
    public Page<BaoXiuDan> findAll(Integer p, int size, Integer repair_man_id) {
        Sort sort = new Sort(Sort.Direction.ASC, "id");

        PageRequest pr = PageRequest.of(p - 1, size, sort);
        return this.baoXiuDanDao.findAll(new Specification<BaoXiuDan>() {
            @Override
            public Predicate toPredicate(Root<BaoXiuDan> h, CriteriaQuery<?> q, CriteriaBuilder c) {
                /*criteriaQuery.where(criteriaBuilder.equal(root.get("repairMan").as(RepairMan.class),repairManDao.findRepairManById(repair_man_id)));//????
                return criteriaQuery.getRestriction();*/
                //
                List<Predicate> pList = new ArrayList<>();

                pList.add(c.equal(h.get("repairMan").as(RepairMan.class),
                        repairManDao.findRepairManById(repair_man_id)));

                Predicate[] p2 = new Predicate[pList.size()];
                q.where(c.and(pList.toArray(p2)));
                return q.getRestriction();

            }
        }, pr);
    }

    @GetMapping(value = "/rm_shenhe", produces = "application/json;charset=utf-8")
    @ResponseBody
    public boolean shenhe(@RequestParam("bxm_id") Integer id, @RequestParam("status") int status
            , @RequestParam(value = "rejectMsg", required = false) String rejectMsg) {
        try {
            RepairMan rm = this.repairManDao.findRepairManById(id);
            rm.setStatus(status);
            rm.setRejectMsg(rejectMsg);
            this.repairManDao.save(rm);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

}